package com.qiwenshare.file.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qiwenshare.common.anno.MyLog;
import com.qiwenshare.common.result.RestResult;
import com.qiwenshare.common.util.DateUtil;
import com.qiwenshare.common.util.HashUtils;
import com.qiwenshare.common.util.security.JwtUser;
import com.qiwenshare.common.util.security.SessionUtil;
import com.qiwenshare.file.api.IUserLoginInfoService;
import com.qiwenshare.file.api.IUserService;
import com.qiwenshare.file.component.InitData;
import com.qiwenshare.file.component.JwtComp;
import com.qiwenshare.file.domain.UserLoginInfo;
import com.qiwenshare.file.domain.user.UserBean;
import com.qiwenshare.file.dto.user.*;
import com.qiwenshare.file.vo.file.FileListVO;
import com.qiwenshare.file.vo.user.UserAuthVo;
import com.qiwenshare.file.vo.user.UserLoginVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Tag(name = "user", description = "该接口为用户接口，主要做用户登录，注册和校验token")
@RestController
@Slf4j
@RequestMapping("/user")
public class UserController {

    @Resource
    IUserService userService;
    @Resource
    IUserLoginInfoService userLoginInfoService;
    @Resource
    JwtComp jwtComp;

    @Resource
    InitData initData;

    public static Map<String, String> verificationCodeMap = new HashMap<>();


    public static final String CURRENT_MODULE = "用户管理";

    @Operation(summary = "用户注册", description = "注册账号", tags = {"user"})
    @PostMapping(value = "/register")
    @MyLog(operation = "用户注册", module = CURRENT_MODULE)
    @ResponseBody
    public RestResult<String> addUser(@Valid @RequestBody RegisterDTO registerDTO) {
        RestResult<String> restResult = null;
        UserBean userBean = new UserBean();
        BeanUtil.copyProperties(registerDTO, userBean);
        restResult = userService.registerUser(userBean, new ArrayList<>());

        return restResult;
    }

    @Operation(summary = "用户登录", description = "用户登录认证后才能进入系统", tags = {"user"})
    @GetMapping("/login")
    @MyLog(operation = "用户登录", module = CURRENT_MODULE)
    @ResponseBody
    public RestResult<UserLoginVo> userLogin(
            @Parameter(description = "登录账号") String loginAccount,
            @Parameter(description = "登录密码") String password) {
        RestResult<UserLoginVo> restResult = new RestResult<UserLoginVo>();
        //String salt = userService.getSaltByTelephone(loginAccount);
        String salt = userService.getSaltByLoginAccount(loginAccount);
        String hashPassword = HashUtils.hashHex("MD5", password, salt, 1024);
        //UserBean result = userService.selectUserByTelephoneAndPassword(telephone, hashPassword);
        UserBean result = userService.selectUserByLoginAccountAndPassword(loginAccount, hashPassword);
        if (result == null) {
            return RestResult.fail().message("账号或密码错误！");
        }

        Map<String, Object> param = new HashMap<>();
        param.put("userId", result.getUserId());
        String token = "";
        try {
            token = jwtComp.createJWT(param);
        } catch (Exception e) {
            log.info("登录失败：{}", e);
            return RestResult.fail().message("创建token失败！");
        }
        //UserBean sessionUserBean = userService.findUserInfoByTelephone(telephone);
        UserBean sessionUserBean = userService.findUserInfoByLoginAccount(loginAccount);
        if (sessionUserBean.getAvailable() != null && sessionUserBean.getAvailable() == 0) {
            return RestResult.fail().message("用户已被禁用");
        }
        UserLoginVo userLoginVo = new UserLoginVo();
        BeanUtil.copyProperties(sessionUserBean, userLoginVo);
        userLoginVo.setToken("Bearer " + token);
        restResult.setData(userLoginVo);
        restResult.setSuccess(true);
        restResult.setCode(200001);
        return restResult;

    }

    @Operation(summary = "检查用户登录信息", description = "验证token的有效性", tags = {"user"})
    @GetMapping("/checkuserlogininfo")
    @ResponseBody
    public RestResult<UserLoginVo> checkUserLoginInfo(@RequestHeader("token") String token) {
        UserLoginVo userLoginVo = new UserLoginVo();
        String userId = userService.getUserIdByToken(token);

        if (StringUtils.isNotEmpty(userId)) {
            LambdaQueryWrapper<UserLoginInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(UserLoginInfo::getUserId, userId);
            lambdaQueryWrapper.likeRight(UserLoginInfo::getUserloginDate, DateUtil.getCurrentTime().substring(0, 10));
            userLoginInfoService.remove(lambdaQueryWrapper);
            UserLoginInfo userLoginInfo = new UserLoginInfo();
            userLoginInfo.setUserId(userId);
            userLoginInfo.setUserloginDate(DateUtil.getCurrentTime());
            userLoginInfoService.save(userLoginInfo);
            UserBean user = userService.getById(userId);
            BeanUtil.copyProperties(user, userLoginVo);
            if (StringUtils.isEmpty(user.getWxOpenId())) {
                userLoginVo.setHasWxAuth(false);
            } else {
                userLoginVo.setHasWxAuth(true);
            }
            return RestResult.success().data(userLoginVo);

        } else {
            return RestResult.fail().message("用户暂未登录");
        }

    }

    @Operation(summary = "检查微信认证", description = "检查微信认证", tags = {"user"})
    @GetMapping("/checkWxAuth")
    @ResponseBody
    public RestResult<Boolean> checkWxAuth() {
        JwtUser sessionUserBean = SessionUtil.getSession();

        if (sessionUserBean != null && !"anonymousUser".equals(sessionUserBean.getUsername())) {
            UserBean user = userService.getById(sessionUserBean.getUserId());

            if (StringUtils.isEmpty(user.getWxOpenId())) {
                return RestResult.success().data(false);
            } else {
                return RestResult.success().data(true);
            }

        } else {
            return RestResult.fail().message("用户暂未登录");
        }

    }

    @Operation(summary = "新增用户", description = "新增用户", tags = {"user"})
    @PostMapping(value = "/add")
    @MyLog(operation = "新增用户", module = CURRENT_MODULE)
    @ResponseBody
    public RestResult<String> add(@Valid @RequestBody AddUserDTO addUserDTO) {
        RestResult<String> restResult = null;
        UserBean userBean = new UserBean();
        BeanUtil.copyProperties(addUserDTO, userBean);
        restResult = userService.registerUser(userBean, addUserDTO.getRoleIds());
        initData.manualInitData();
        return restResult;
    }


    @Operation(summary = "编辑用户", description = "编辑用户", tags = {"user"})
    @PostMapping(value = "/modify")
    @MyLog(operation = "编辑用户", module = CURRENT_MODULE)
    @ResponseBody
    public RestResult<String> modify(@Valid @RequestBody ModifyUserDTO modifyUserDTO) {
        RestResult<String> restResult = null;
        UserBean userBean = new UserBean();
        BeanUtil.copyProperties(modifyUserDTO, userBean);
        restResult = userService.mdoifyUser(userBean, modifyUserDTO.getRoleIds());
        initData.manualInitData();
        return restResult;
    }

    @Operation(summary = "删除用户", description = "删除用户", tags = {"user"})
    @PostMapping(value = "/delete")
    @MyLog(operation = "删除用户", module = CURRENT_MODULE)
    @ResponseBody
    public RestResult<String> delete(@Valid @RequestBody DeleteUserDTO dto) {
        RestResult<String> restResult = null;
        UserBean userBean = new UserBean();
        BeanUtil.copyProperties(dto, userBean);
        restResult = userService.deleteUser(userBean);
        initData.manualInitData();
        return restResult;
    }

    @Operation(summary = "重置用户密码", description = "重置用户密码", tags = {"user"})
    @PostMapping(value = "/resetPassword")
    @MyLog(operation = "重置用户密码", module = CURRENT_MODULE)
    @ResponseBody
    public RestResult<String> resetPassword(@Valid @RequestBody DeleteUserDTO modifyUserDTO) {
        RestResult<String> restResult = null;
        UserBean userBean = new UserBean();
        BeanUtil.copyProperties(modifyUserDTO, userBean);
        restResult = userService.resetPassword(userBean);

        return restResult;
    }

    @Operation(summary = "修改用户密码", description = "修改用户密码", tags = {"user"})
    @PostMapping(value = "/modifyPassword")
    @MyLog(operation = "修改用户密码", module = CURRENT_MODULE)
    @ResponseBody
    public RestResult<String> modifyPassword(@Valid @RequestBody ModifyPasswordDTO modifyPasswordDTO) {
        return userService.modifyPassword(modifyPasswordDTO);
    }


    @Operation(summary = "分页查询用户", description = "分页查询用户", tags = {"user"})
    @GetMapping(value = "/page")
    @MyLog(operation = "分页查询用户", module = CURRENT_MODULE)
    @ResponseBody
    public RestResult<UserLoginVo> queryPage(
            @Parameter(description = "用户名", required = false) String username,
            @Parameter(description = "工号", required = false) String workOrder,
            @Parameter(description = "登录账号", required = false) String loginAccount,
            @Parameter(description = "当前页", required = true) long currentPage,
            @Parameter(description = "页面数量", required = true) long pageCount) {
        IPage<UserLoginVo> userPage = userService.getUserList(username, loginAccount, workOrder, currentPage, pageCount);
        List<UserLoginVo> userLoginVoList = userPage.getRecords().stream().peek(u -> {
            u.setDepartmentName(InitData.getDepartmentNameById(u.getDepartmentId()));
            u.setJobName(InitData.getJobNameById(u.getJobId()));
        }).collect(Collectors.toList());
        return RestResult.success().dataList(userLoginVoList, userPage.getTotal());
    }

    @Operation(summary = "查询用户信息", description = "查询用户信息", tags = {"user"})
    @GetMapping(value = "/getAuthInfo")
    @MyLog(operation = "查询用户信息", module = CURRENT_MODULE)
    @ResponseBody
    public RestResult<UserAuthVo> getAuthInfo() {
        return RestResult.success().data(userService.getAuthInfo());
    }
}
